Vue.component('company-autocomplete', {
  template: `
    <el-autocomplete
      v-if="!dataId"
      v-model="form.company_name"
      class="inline-input"
      :fetch-suggestions="companyQuerySearch"
      placeholder="公司名称"
      :trigger-on-focus="false"
      :maxlength="100"
      @select="companyHandleSelect"
    >
        <template slot="append">
          <a @click="companyHandleSelect">查询工商信息</a>
        </template>
    </el-autocomplete>
    <el-input v-else v-model="form.company_name" readonly show-word-limit :maxlength="100">
      <template slot="append">
        <a @click="companyHandleSelect">查询工商信息</a>
      </template>
    </el-input>
  `,
  props: {
    form: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      dataId: undefined,
      // 天眼查查询条件
      tycParam: {
        id: '',
        companyName: '',
        tycId: ''
      }
    }
  },
  mounted() {
    this.dataId = window.parent.dataId
  },
  methods: {
    // 标的公司查询
    companyQuerySearch(queryString, cb) {
      axios({
        url: window.parent.baseUrl + '/company/companyBase/companyListByName?companyName=' + queryString,
        headers: {
          'Content-Type':'application/json;charset=utf-8',
          'Authorization': window.parent.token
        },
        method: 'get'
      }).then(res => {
        let _res = res.data
        if (_res.code === 200) {
          cb(_res.data)
        }
      })
    },
    // 标的公司选择后事件
    companyHandleSelect(item) {
      var companyName = this.form.company_name
      if (companyName === '' || companyName === undefined) {
        this.$message({ showClose: true, message: '请先输入公司全称！', type: 'error' })
        return
      }
      this.tycParam.id = this.form.id
      this.tycParam.tycId = this.form.tycId || item.id
      this.tycParam.companyName = companyName
      axios({
        url: window.parent.baseUrl + '/company/companyBase/companyDetailsByName',
        headers: {
          'Content-Type':'application/json;charset=utf-8',
          'Authorization': window.parent.token
        },
        method: 'get',
        params: this.tycParam
      }).then(res => {
        let _res = res.data
        if (_res.code === 200) {
          if (_res.msg === 'exist') {
            this.form.company_name = ''
            this.$message({ showClose: true, message: '公司已存在,请重新录入！', type: 'error' })
          } else if (_res.msg === 'nofind') {
            this.form.company_name = ''
            this.$message({ showClose: true, message: '天眼查没有获取到该公司信息！', type: 'error' })
          } else {
            this.formCleaing()
            // 公司简称
            this.form.company_short_name = _res.data.companyShortName || _res.data.companyName
            // 公司名称
            this.form.company_name = _res.data.companyName
            // 企业统一社会信用代码
            this.form.credit_code = _res.data.creditCode
            // 法人代表
            this.form.legal_person = _res.data.legalPerson
            // 成立时间
            this.form.set_date = this.formatDate(_res.data.setDate)
            // 一级行业
            this.form.industry_first = _res.data.industryFirst
            this.form.industry_second = _res.data.industrySecond
            // this.$refs.industry_zj.initIndustrySecond()
            // 注册地-省
            this.form.province_registered = _res.data.provinceRegistered
            this.form.city_registered = _res.data.cityRegistered
            // this.$refs.cityZj.initCity()
            // 注册地-详细地址
            this.form.details_registered = _res.data.detailsRegistered
            // 注册资本金额
            this.form.capital_registered = _res.data.capitalRegistered
            // 注册资本币种
            this.form.currency = _res.data.currency
            // 经营范围
            this.form.business_scope = _res.data.businessScope
            // 天眼查ID
            this.form.tyc_id = _res.data.tycId
            // 公司logo
            this.form.logo_info = _res.data.logo
            // 企业简介
            this.form.remark = _res.data.remark
            this.$message({ showClose: true, message: '已获取工商信息！', type: 'success' })
          }
        }
      })
    },
    formCleaing() {
      this.form.company_short_name = ''
      this.form.credit_code = ''
      this.form.legal_person = ''
      this.form.set_date = ''
      this.form.industry_first = ''
      this.form.industry_second = ''
      this.form.province_registered = ''
      this.form.city_registered = ''
      this.form.details_registered = ''
      this.form.capital_registered = ''
      this.form.business_scope = ''
      this.form.currency = ''
      this.form.tyc_id = ''
      this.form.logo_info = ''
    },
    formatDate(date) {
      // date = new Date();
      date = new Date(date); //转换成Data();
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? '0' + m : m;
      var d = date.getDate();
      d = d < 10 ? ('0' + d) : d;
      return y + '-' + m + '-' + d;
      }
  }

})
